/**
 * @Title: LoginController
 * @Package cn.com.yiqi.wsc.portal.controller.admin
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-胥钰淋
 * @date 2017/2/13 18:00
 * @version V1.0
 */
package cn.com.yiqi.wsc.portal.controller.admin;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.portal.common.Setting;
import cn.com.yiqi.wsc.portal.common.SettingUtils;

import java.util.Map;

import cn.com.yiqi.wsc.service.captcha.CaptchaService;
import cn.com.yiqi.wsc.service.weixin.impl.WeixinPayImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 会员系统组-胥钰淋
 * @ClassName: LoginController
 * @Description: TODO
 * @date 2017/2/13 18:00
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    WeixinPayImpl weixinPay;

    @Autowired
    private CaptchaService captchaService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("admin/login");
        return mv;
    }

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ResponseBody
    public Result submit(@RequestParam String username, @RequestParam String password, @RequestParam String verify, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
/*        if ("admin".equalsIgnoreCase(username)) {
            Setting setting = SettingUtils.get();*/


        //  判断验证码
        if (!captchaService.validate(request, verify)) {
            result.setErrMsg("验证码错误");
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            return result;
        }

        session.setAttribute("username", username);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        try {
            //4、登录，即身份验证
            subject.login(token);
            result.setStatus(ResultStatusEnum.操作成功.getCode());
        } catch (AuthenticationException e) {

            e.printStackTrace();

            if (e instanceof IncorrectCredentialsException) {
                //密码校验错误
                return ResultUtil.paramError(result, "用户名或密码错误");
            }

            if (e instanceof UnknownAccountException) {
                //账号不存在
                return ResultUtil.paramError(result, "用户名或密码错误");
            }
            if (e instanceof LockedAccountException) {
                //账号被禁用
                return ResultUtil.paramError(result, "账号被禁用");
            }

            return ResultUtil.fail(result, "服务器异常");

        }
/*        }
        result.setStatus(1);*/
        return result;
    }

    @RequestMapping(value = "/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login";
    }

}
